import com.baowenli.pojo.*;
import com.baowenli.server.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;


/**
 * @return
 */// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class APP {

    private static final String MANGER_LOGIN = "1";
    private static final String WORKER_LOGIN = "2";
    private static final String EXIT1 = "3";

    static int workId = 0;
    static WorkerService workerService = new WorkerService();
    static OfficalTypeService officalTypeService = new OfficalTypeService();
    static HousrInfoService housrInfoService=new HousrInfoService();
    static HandleHouseService handleHouseService = new HandleHouseService();
    static CheckInService checkInService = new CheckInService();
    static OrderInfoService orderInfoService = new OrderInfoService();

    public static void main(String[] args) {

        //创建集合
        while(true){
            System.out.println("\t\t\t\t欢迎来到佐酒店管理管理系统\t\t\t\t");
            System.out.println("\t\t\t\t1:管理员登录");
            System.out.println("\t\t\t\t2:操作员登录");
            System.out.println("\t\t\t\t3:退出");
            Scanner sc = new Scanner(System.in);
            System.out.print("\t\t\t\t请进行选择：");
            String choose = sc.next();
            switch (choose) {
                case MANGER_LOGIN -> mangerLogin();
                case WORKER_LOGIN -> workerLogin();
                case EXIT1 -> {
                    System.out.println("\t\t\t\t退出");
                    //break loop;
                    System.exit(0);
                }
                default -> System.out.println("\t\t\t\t没有这个选项");
            }
        }

    }

    /**
     *  管理员登录
     *
     * */
    public static void mangerLogin(){
        System.out.print("\t\t\t\t请输入管理员账号：");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        System.out.print("\t\t\t\t请输入管理员密码：");
        String password = sc.next();
        Worker findwork = workerService.selectByIdManget(username,password);
        if(findwork!=null){
            System.out.println("\t\t\t\t恭喜您登录成功");
            loop:
            while (true) {
                System.out.println("\t\t\t\t欢迎来到佐酒店管理系统管理员版\t\t\t\t");
                System.out.println("\t\t\t\t1:查看官网预定信息");
                System.out.println("\t\t\t\t2:办理住房登记表");
                System.out.println("\t\t\t\t3:办理退房登记表");
                System.out.println("\t\t\t\t4:进行解雇操作员");
                System.out.println("\t\t\t\t5:进行查看所有操作员信息");
                System.out.println("\t\t\t\t6:进行查看订单信息");
                System.out.println("\t\t\t\t7:进行查看住房退房表");
                System.out.println("\t\t\t\t8:进行操作员升迁");
                System.out.println("\t\t\t\t9:退出");
                System.out.print("\t\t\t\t请输入您的选择：");

                String choose = sc.next();
                switch (choose) {
                    case "1" -> viewOfficalWebInfo();
                    case "2" -> transactShelterTable();
                    case "3" -> transactAbortTable();
                    case "4" -> reliveWorker();
                    case "5" -> selectAllWorker();
                    case "6" -> selectAllOrderInfo();
                    case "7" -> selectAllEnterRoomInfo();
                    case "8" -> workPromote();
                    case "9" -> {
                        System.out.println("退出");
                        //break loop;
                        System.exit(0);
                    }
                    default -> System.out.println("没有这个选项");
                }
            }
        }else{
            System.out.println("登录失败");

        }
    }

    /**
     *  操作员升迁
     *
     * */
    public static void workPromote(){
        System.out.print("\t\t\t\t请输入操作员用户名:");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        System.out.print("\t\t\t\t请输入操作员密码:");
        String password = sc.next();
        int temp = workerService.workerPromote(username, password);
        if(temp==1){
            System.out.println("\t\t\t\t"+username+"已经成功晋升");
        }else{
            System.out.println("\t\t\t\t"+username+"晋升失败");
        }

    }
    /**
     *  操作员登录
     *
     * */
    public static void workerLogin(){
        System.out.print("\t\t\t\t请输入操作员账号：");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();

        System.out.print("\t\t\t\t请输入操作员密码：");
        String password = sc.next();

        Worker findwork = workerService.selectById(username,password);

        workId = findwork.getWorkerId();
        if(findwork!=null){
            System.out.println("\t\t\t\t恭喜您登录成功");
        loop:
        while (true) {
            System.out.println("\t\t\t\t欢迎来到佐酒店管理系统操作员版\t\t\t\t");
            System.out.println("\t\t\t\t1:查看官网预定信息");
            System.out.println("\t\t\t\t2:办理住房登记表");
            System.out.println("\t\t\t\t3:办理退房登记表");
            System.out.println("\t\t\t\t4:退出");
            System.out.print("\t\t\t\t请输入您的选择：");

            String choose = sc.next();
            switch (choose) {
                case "1" -> viewOfficalWebInfo();
                case "2" -> transactShelterTable();
                case "3" -> transactAbortTable();
                case "4" -> {
                    //break loop;
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
        }else{
            System.out.println("\t\t\t\t登录失败");
        }
    }



    /**
     * 查看官网预定信息
     *
     */
    public static void viewOfficalWebInfo(){
        List<OrderType> orderTypes = officalTypeService.selectAllOfficalInfo();
        orderTypes.forEach(new Consumer<OrderType>() {
            @Override
            public void accept(OrderType orderType) {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format(orderType.getCreateTime());
                System.out.println("\t"+"预定方式:"+orderType.getType()+"\t\t"+"留言:"+orderType.getRemark()+"\t" +
                        "\t"+time);
            }
        });
    }


    /**
     *  办理住房手序
     */
    public static void transactShelterTable()  {

        System.out.println("\t\t\t\t查看房间信息");
        List<Room> rooms = housrInfoService.selectAllHouseInfo();

        rooms.forEach(new Consumer<Room>() {
            @Override
            public void accept(Room room) {
                System.out.print("\t\t房间号码是："+room.getRoomNumber()+"\t房间ID是："+room.getRoomId()+"\t房间类型是："+room.getRoomType()+
                        "\t" +
                        "房间价格是："+room.getRoomPrice()+"\t房间状态是:");
                if(room.getRoomStatus()==1){
                    System.out.print("可以预定");
                } else if (room.getRoomStatus()==0) {
                    System.out.print("已经被被预定");
                }else if(room.getRoomStatus()==-1){
                    System.out.print("已经被入住");
                }else if(room.getRoomStatus()==2){
                    System.out.print("可以入住");
                }else{
                    System.out.print("房间维修中");
                }
                System.out.print("\t房间类型Id:"+room.getTypeId());
                System.out.println();
            }
        });
        Scanner sc = new Scanner(System.in);
        Room room = new Room();
        int roomNumber =0;
        int roomID = 0;
        while(true){
            System.out.println("\t\t\t\t请选择你要办理的房间");
            //房间号 id号

            System.out.print("\t\t\t\t请选择房间号：");
            roomNumber= sc.nextInt();

            System.out.print("\t\t\t\t请选择房间ID：");
            roomID = sc.nextInt();
            room = housrInfoService.selectByIdToPrice(roomID);

            if(room.getRoomStatus()==1){
                break;
            }else if(room.getRoomStatus()==0){
                System.out.print("\t\t\t\t该房间已经被预定：");
                System.out.println("请重新选择房间");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                continue;
            } else if (room.getRoomStatus() == -1) {
                System.out.print("\t\t\t\t该房间已经被入住：");
                System.out.println("请重新选择房间");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                continue;
            }else if(room.getRoomStatus()==2){
                break;
            }else{
                System.out.print("\t\t\t\t该房间正在维修中：");
                System.out.println("\t\t请重新选择房间");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                continue;
            }
        }

        System.out.print("\t\t\t\t请输入入住人数：");
        int count = sc.nextInt();

        StringBuffer nameArr = new StringBuffer();
        StringBuffer idCardArr = new StringBuffer();

        //入住人Id
        String entername  = "";
        int enterId = 0;
        String enterphone = "";
        //一次人数
        for (int i = 1; i <= count; i++) {
            System.out.print("\t\t\t\t请输第"+i+"个入住人id：");
            int id = sc.nextInt();
            enterId = id;
            System.out.print("\t\t\t\t请输第"+i+"个入住人姓名：");
            String name = sc.next();
            entername = name;
            System.out.print("\t\t\t\t请输第"+i+"个入住人性别：");
            String gender = sc.next();

            System.out.print("\t\t\t\t请输第"+i+"个入住人手机号：");
            String phone = sc.next();
            enterphone = phone;

            System.out.print("\t\t\t\t请输第"+i+"个入住人家庭地址：");
            String address = sc.next();
            System.out.print("\t\t\t\t请输第"+i+"个入住人身份证号：");
            String idCard = sc.next();
            nameArr.append(name);
            idCardArr.append(idCard);
            int temp = handleHouseService.insertHousePeopleInfo(id,name, gender, phone, address,idCard);

            if(temp==1){
                System.out.println("\t\t\t\t恭喜添加成功");
            }else{
                System.out.println("\t\t\t\t添加失败");
            }
        }

        String nameArr1 = nameArr.toString();
        String idCardArr1 = idCardArr.toString();
        System.out.print("\t\t\t\t请输入订单号进行产生订单:");
        int orderId = sc.nextInt();
        System.out.println("\t\t\t\t操作员id:"+workId);
        //产生订单
        int temp1 = orderInfoService.insertOrderInfo(orderId, 5, "前台预定", enterId, entername, enterphone,
                room.getRoomId(), room.getRoomPrice(),workId,room.getRoomId());

        //进行住房登记
        int temp = checkInService.insertHousePeopleInfo(orderId, roomID, roomNumber, count, nameArr1, idCardArr1);

        if(temp==1){
            System.out.println("\t\t\t\t恭喜住房登记成功");
        }
        if(temp1==1){
            System.out.println("\t\t\t\t订单生成成功");
        }

        //设定房间状态为 已经不可预定
        Boolean aBoolean = housrInfoService.alterRoomStatus(room.getRoomId(),-1);

    }

    /**
     *  办理退房手续
     */
    public static void transactAbortTable(){
        System.out.print("\t\t\t\t请输入房间ID：");

        Scanner sc = new Scanner(System.in);
        int roomId = sc.nextInt();
        System.out.print("\t\t\t\t请输入订单ID:");
        int orderId = sc.nextInt();

        //进行退房登记
        int temp = checkInService.exitHomeRegister(roomId,orderId);

        if(temp==1){
            System.out.println("\t\t\t\t恭喜您已经成功退房");
        }

        //算出住房天的钱数
        CheckIn check = checkInService.findHomeCountDay(roomId,orderId);
        Date checkIn = check.getCheckInTime();
        Date checkOut = check.getCheckOutTime();

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String before = simpleDateFormat.format(checkIn);
        String after = simpleDateFormat.format(checkOut);



        String[] beforeArr = before.split("-");
        String[] afterArr = after.split("-");

        //总共住的天数
        int dateCount =0;
        int dateArr[] = new int[]{365,30,1};
        for (int i = 0; i < beforeArr.length; i++) {
            dateCount =  dateCount+(Integer.parseInt(beforeArr[i]) - Integer.parseInt(afterArr[i]))*dateArr[i];
        }

        if(dateCount<=0){
            dateCount=1;
        }

        Order order = orderInfoService.findHomePrice(roomId,orderId);
        System.out.println("\t\t\t\t请接收您的订单：");
        System.out.println("\t\t\t\t您的姓名："+order.getName()+"\t您的预定方式是："+order.getOrderType()+"\t您住的房号是："+check.getRoomNumber()+"\t您住的天数为："+dateCount+"\t您应支付的价钱是："+order.getOrderCost()*dateCount);

        System.out.println("\t\t\t\t欢迎下次光临本酒店：");

        //设定房间状态为 已经不可预定
        Boolean aBoolean = housrInfoService.alterRoomStatus(Integer.parseInt(check.getRoomNumber()),1);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *  进行解雇操作员
     *
     * */
    public static void reliveWorker() {
        System.out.print("\t\t请输入操作员姓名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        System.out.print("\t\t请输入操作员姓名：");
        String password = sc.next();
        int temp = workerService.reliveWorker(name, password);
        if(temp==1){
            System.out.println("\t\t操作员："+name+"已经被解雇");
        }else{
            System.out.println("\t\t解雇失败");
        }
    }

    /**
     *  查看所有操作员信息
     */
    public static void selectAllWorker(){
        System.out.println("\t\t所有的操作员信息为");
        List<Worker> workers = workerService.selectAll();
        workers.forEach(new Consumer<Worker>() {
            @Override
            public void accept(Worker worker) {
                System.out.println("\t\t操作员姓名："+worker.getName()+"\t操作员性别："+worker.getGender()+"\t操作员手机号："+worker.getPhone()+"\t操作员地址："+worker.getAddress());
            }
        });
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    /**
    *  查看所有订单信息
    */
    public static void selectAllOrderInfo(){
        Scanner sc = new Scanner(System.in);
        System.out.print("\t\t请输入查看日期: ");
        String date = sc.next();
        while(true){
            System.out.println("\t\t1:这个日期的之前的订单：");
            System.out.println("\t\t2:这个日期的之后的订单：");
            System.out.println("\t\t3:这个日期的订单：");
            System.out.print("\t\t请输入选择：");
            String choose = sc.next();
            switch (choose) {
                case "1" -> orderInfoService.selectAllOrderInfoAfter(date);
                case "2" -> orderInfoService.selectAllOrderInfoBefore(date);
                case "3" -> orderInfoService.selectAllOrderInfoNow(date);
                default -> System.out.println("没有这个选项");
            }
            break;
        }
    }

    /**
     *  查看所有住房登记表
     *
     * */
    public static void selectAllEnterRoomInfo(){
        List<CheckIn> checks = checkInService.findAllEnterRoomInfo();
        checks.forEach(new Consumer<CheckIn>() {

            @Override
            public void accept(CheckIn checkIn) {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String before = simpleDateFormat.format(checkIn.getCheckInTime());
                String after = simpleDateFormat.format(checkIn.getCheckOutTime());
                System.out.println("\t\t该房间号是："+checkIn.getRoomNumber()+"\t该房间住的人数是"+checkIn.getPeoCount()+"" +
                        "\t该房间住的人是"+checkIn.getPersons()+"\t该房间办理入住时间是："+before+"\t该房间办理退房时间是："+after);
            }
        });
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


}